package com.nimt.hqgl_idea.mapper;


import com.nimt.hqgl_idea.entity.Merchant;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Component(value = "MerchantMapper")
public interface MerchantMapper {
    @Select("<script>" +
            "select * from merchant" +
            "<trim prefix = 'where' prefixOverrides = 'AND|OR'>" +
            "<if test='keywords!=null'> and concat(merchantName,bossName,merchantType) like concat('%',#{keywords},'%')</if>" +
            "</trim>" +
            " limit #{start},#{pageSize}" +
            "</script>")
    List<Merchant> selectAll(@Param("start") Integer start,
                             @Param("pageSize") Integer pageSize,
                             @Param("keywords") String keywords);

    @Select("<script>" +
            "select count(1) from merchant " +
            "<trim prefix = 'where' prefixOverrides = 'AND|OR'>" +
            "<if test='keywords!=null'> and merchantName like concat('%',#{keywords},'%')</if>" +
            "</trim>" +
            "</script>")
    int count(@Param("keywords") String keywords);

    @Select("select * from merchant where id = #{id}")
    Merchant selectById(@Param("id") Integer id);

    @Update("<script>" +
            "update merchant " +
            "<trim prefix='set' suffix='where id=#{id}' suffixOverrides=','>" +
            "<if test='merchantName!=null'> merchantName=#{merchantName},</if>" +
            "<if test='bossName!=null'> bossName=#{bossName},</if>" +
            "<if test='merchantType!=null'> merchantType=#{merchantType},</if>" +
            "<if test='state!=null'> state=#{state},</if>" +
            "</trim>" +
            "</script>")
    int update(Merchant merchant);

    @Delete("delete from merchant where id = #{id}")
    int delete(Integer id);

    @Insert("insert into merchant values(null,#{merchantName},#{bossName},now(),#{merchantType},0)")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void add(Merchant merchant);
}
